﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace WpfApplication3
{
    /// <summary>
    /// Logique d'interaction pour MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public int[] aget = new int[290];
        public int[] datt = new int[290];
        public int[] nbrt = new int[290];
        public int[] rest = new int[290];
        public int ss = 1;//seuil
        public int nc = 100;
        public double[,] we = new double[4, 100];//j,i
        public double[] ws = new double[100];
        public double[] ph = new double[100];
        public double py = 0;
        public double[] sh = new double[100];
        public double[] dh = new double[100];
        public double[] zh = new double[100];
        public double sy = 0;
        public double y = 0;
        public double ty = 0;
        int ii = 0;//290
        double ee = 0;// Erreur

        public MainWindow()
        {
            InitializeComponent();
            lab1.Opacity = 0;
            lab2.Opacity = 0;
            lab3.Opacity = 0;
            lab4.Opacity = 0;
            age.Opacity = 0;
            Date.Opacity = 0;
            nbr.Opacity = 0;
            pred.Opacity = 0;
            res.Opacity = 0;
            apren.Opacity = 0;
            pred.Height = 0;
            apren.Height = 0;
            //Console.Write("  ********** " + ws[0]);
            ajouter.Opacity = 0;
            ajouter.Height = 0;
        }


        private void Apprentissage(object sender, RoutedEventArgs e)
        {
           
            lab1.Opacity = 1; lab1.Content = "donnee le nom du fichier";
            lab2.Opacity = 0;
            lab3.Opacity = 0;
            lab4.Opacity = 0;
            age.Opacity = 1;
            age.Text = "c:\\res_neuron_apprentissage.txt";
            Date.Opacity = 0;
            nbr.Opacity = 0;
            pred.Opacity = 0;
            pred.Height = 0;
            res.Opacity = 0;
            apren.Opacity = 1;
            apren.Height = 39;
           
        }

        private void Reconnaissance(object sender, RoutedEventArgs e)
        {
            lab1.Opacity = 1; lab1.Content = "donnee l'age";
            lab2.Opacity = 1;
            lab3.Opacity = 1;
            lab4.Opacity = 1;
            age.Opacity = 1; age.Text = "";
            Date.Opacity = 1;
            nbr.Opacity = 1; nbr.Text = "";
            pred.Opacity = 1;
            pred.Height = 39;
            res.Opacity = 1;
            apren.Opacity = 0;
            apren.Height = 0;
            ajouter.Opacity = 1;
            ajouter.Height = 39;


        }
        private void ajout(object sender, RoutedEventArgs e)
        {
            StreamWriter file = new StreamWriter("nouvellesvaleurs.txt");
           
            lab1.Opacity = 0;
            lab2.Opacity = 0;
            lab3.Opacity = 0;
            lab4.Opacity = 0;
            age.Opacity = 0;
            Date.Opacity = 0;
            nbr.Opacity = 0;
            pred.Opacity = 0;
            res.Opacity = 0;
            apren.Opacity = 0;
            pred.Height = 0;
            apren.Height = 0;
            Int32 aa = Convert.ToInt32(age.Text);
            Int32 dd = Convert.ToInt32(Date.Text);
            Int32 nn = Convert.ToInt32(nbr.Text);
            Int32 rr = Convert.ToInt32(res.Text);
           // if (!char.IsLetter(e.KeyChar))
           // {

           // }

        }
        private void pred_Click(object sender, RoutedEventArgs e)
        {
            int a = Convert.ToInt32(age.Text);
            int d = Convert.ToInt32(Date.Text);
            int n = Convert.ToInt32(nbr.Text);
           
            // Etape 2 propagation 
            // Potentiel in
            for (int i = 0; i < nc; i++)
            {
                ph[i] = (a * we[0, i]) + (d * we[1, i]) + (n * we[2, i]) + (we[3, i]);
            }
            //calcule de signal (signoide de parametre 1)in
            for (int i = 0; i < nc; i++)
            {
                sh[i] = 1 / (1 + (Math.Exp(-ph[i])));
            }
            // Potentiel out
            py = 0;
            for (int i = 0; i < nc; i++)
            {
                py = py + (sh[i] * ws[i]);
            }
            //calcule de signal (signoide de parametre 1)out
            sy = 1 / (1 + (Math.Exp(-py)));//sorti ***
            Console.Write("ee = "); Console.Write(ee); Console.Write("  ********** " + ws[0]);
            res.Text = Convert.ToString(sy); Console.Write("we = "); Console.Write(sy);
            //y = sy * (1 - sy);
            //ee = 0.5 * ((y - sy) * (y - sy));
            
            }
        private void Quitter(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();   
        }

        private void apren_Click(object sender, RoutedEventArgs e)
        {
            String line=null;
            
            int j;
            try
            {
                //Pass the file path and file name to the StreamReader constructor 
                StreamReader sr = new StreamReader(age.Text);

                //Lire la premiere ligne du fichier 

                
                //Continuer la lecture jusqu'a la fin du fichier 
                j = 0;
                do
                {
                    //lecture du ligne du texte 
                    line = sr.ReadLine();
                    

                    aget[j] = ((Convert.ToInt32(line[0])-48)*10) + ((Convert.ToInt32(line[1]))-48);
                        datt[j] = (((Convert.ToInt32(line[3]))-48)*10) + ((Convert.ToInt32(line[4]))-48);
                        nbrt[j] = (Convert.ToInt32(line[6]))-48;
                        if (line[7] != ',') nbrt[j] = nbrt[j]*10 + ((Convert.ToInt32(line[7]))-48);
                        rest[j] =Convert.ToInt32(line[line.Length-1])-49;
                        //Console.Write(aget[j] + " " +  datt[j] + " " + nbrt[j] + " " + rest[j]+"\n");
                    j++;
                } while (j<=289);
                //age.Text = Convert.ToString(nbrt[289]);
                //nbr.Text = Convert.ToString(nbrt[0]);
                //Fermiture du fichier 
                sr.Close();
                
            }
            catch (Exception z)
            {
                nbr.Text = "Erreur";
                Console.WriteLine("Exception: " + z.Message);
            }
           
            age.Text = "aprentissage effectué";
            nbr.Opacity = 1;
             //initialisation
            Random rand = new Random();
            int sin = 0;
            for (int i = 0; i < 4; i++)
            {
                for (j = 0; j < nc; j++)
                {
                    sin = rand.Next(500);
                    we[i,j] = rand.Next(500000);
                    we[i, j] = we[i, j] * 0.000001;
                    if (sin % 2 == 0) we[i, j] = we[i, j] * (-1);
                    //Console.Write(we[i,j]);
                }
            }
            for (int k = 0; k < nc; k++)
            {
                sin = rand.Next(500);
                ws[k] = rand.Next(500000);
                ws[k] = ws[k] * 0.000001;
                if (sin % 2 == 0) ws[k] = ws[k] * (-1);
                //Console.Write(ws[k]);

            }
            ii=0;//290
            ee=0;// Erreur
            //un iteration
            do{
                do{
            // Etape 2 propagation 
            // Potentiel in
            for (int i = 0; i < nc; i++)
            {
                ph[i] = (aget[ii] * we[0, i]) + (datt[ii] * we[1, i]) + (nbrt[ii] * we[2, i]) + (ss * we[3, i]);
            }
            //calcule de signal (signoide de parametre 1)in
            for (int i = 0; i < nc; i++)
            {
                sh[i] = 1 / (1 + (Math.Exp(-ph[i])));
            }
            // Potentiel out
            py = 0;
            for (int i = 0; i < nc; i++)
            {
                py = py + (sh[i] * ws[i]);
            }
            //calcule de signal (signoide de parametre 1)out
            sy = 1 / (1 + (Math.Exp(-py)));//sorti ***
            //Etape 3 calcule de l'erreur
            ee = 0.5 * ((rest[ii] - sy ) * (rest[ii] - sy));
            if (ee < 0.001) break;
            // si l'erreur et ok stop si non on continue
            //Etape 4 calcule de signale d'erreur sur la couche de sortie
            y = sy * (1 - sy);
            ty = (rest[ii]-sy) * y;
            //Etape 5 calcule de signale d'erreur sur la couche cachée
            //calcule de derivée
            for (int i = 0; i < nc; i++)
            {
                dh[i] = sh[i]*(1-sh[i]);
            }
            //
            for (int i = 0; i < nc; i++)
            {
                zh[i] = dh[i] * (ty * ws[i]);//(ty * ws[i]) a verifier
            }
            //Etape 6 correection des poid de la couche de sortie
            
            for (int i = 0; i < nc; i++)
            {
                ws[i] = ws[i] + (ty * sh[i]);
            }
            //Etape 6 correection des poid de la couche de sortie
            for (int i = 0; i < nc; i++)
            {
                we[0, i] = we[0, i] + (zh[i] * aget[ii]);// age
            }
            for (int i = 0; i < nc; i++)
            {
                we[1, i] = we[1, i] + (zh[i] * datt[ii]);//date
            }
            for (int i = 0; i < nc; i++)
            {
                we[2, i] = we[2, i] + (zh[i] * nbrt[ii]);//nbr
            }
            for (int i = 0; i < nc; i++)
            {
                we[3, i] = we[3, i] + (zh[i] * ss);//seuil
            }
                    
                } while (ee > 0.001);
            ii++;
            Console.Write(ii); Console.Write("   "); Console.Write(sy); Console.Write("   "); Console.Write(ee); Console.Write('\n');
            }while(ii < 277 );
            //Console.Write("  ********** " + ws[0]);
            Console.Write("we1 = "); Console.Write(we[1, 1]); 
            }

       

    }
}
